//
//  THCustomTabBar.m
//  YJB
//
//  Created by YJB on 2016/11/26.
//  Copyright © 2016年 YJB. All rights reserved.
//

#import "THCustomTabBar.h"
#import "masonry.h"
#import "THTabBarButton.h"

@interface THCustomTabBar ()
@property(nonatomic,weak)UIButton *selectBtn;
@property(nonatomic,weak)UIButton *firstBtn;
@end
@implementation THCustomTabBar
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        UIImageView *bgImageView = [[UIImageView alloc]init];
        [self addSubview:bgImageView];
        bgImageView.backgroundColor = kUIColorFromRGB(0xfa5f3c);
        [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.height.mas_equalTo(0.5);
        }];
        
        NSArray *images = @[@"cpc_home_icon",@"cpc_publish",@"cpc_join"];
        NSArray *selectImages = @[@"cpc_home_icon_select",@"cpc_publish_select",@"cpc_join_select"];
        NSArray *titles = @[@"CPC大厅",@"我发布的",@"我参与的"];
        NSMutableArray *buttons = [NSMutableArray array];
        for (int i = 0; i < titles.count; i ++)
        {
            THTabBarButton *btn = [[THTabBarButton alloc]init];
            btn.tag = 10 + i;
            if (i == 0)
                self.firstBtn = btn;
            
            [btn setImage:Image(images[i]) forState:UIControlStateNormal];
            [btn setImage:Image(selectImages[i]) forState:UIControlStateDisabled];
            [btn setTitle:titles[i] forState:UIControlStateNormal];
            [btn setTitleColor:kUIColorFromRGB(0xfa5f3c) forState:UIControlStateDisabled];
            [btn setTitleColor:kUIColorFromRGB(0x999999) forState:UIControlStateNormal];
            [self addSubview:btn];
            [buttons addObject:btn];
            if (i == 0)
            {
                self.selectBtn = btn;
                btn.enabled = NO;
            }
            
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        
        [buttons mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(0);
            make.height.mas_equalTo(44);
        }];
        [buttons mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:7.5 * GRIDW leadSpacing:20 tailSpacing:20];
        
        [NOTIFICATIONCENTER addObserver:self selector:@selector(selectItem) name:THFlowEarnPublishSuccessNotification object:nil];
    }
    return self;
}

// 从任务大厅、我参与的界面进入发布界面，发布成功时跳到我发布的界面
- (void)selectItem
{
    THCustomTabBarItemType type = THCustomTabBarItemTypeMyPublish;
    UIButton *publishBtn = [self viewWithTag:11];
    if (_selectBtn.tag == publishBtn.tag) {
        return;
    }
    _selectBtn.enabled = YES;
    publishBtn.enabled = NO;
    _selectBtn = publishBtn;
    if (self.tabBarItemClickBlock) {
        self.tabBarItemClickBlock(type);
    }
}

- (void)btnClick:(UIButton *)button
{
    _selectBtn.enabled = YES;
    button.enabled = NO;
    _selectBtn = button;
    THCustomTabBarItemType type = 0;
    switch (button.tag)
    {
        case 10:
            type = THCustomTabBarItemTypeHall;
            break;
        case 11:
            type = THCustomTabBarItemTypeMyPublish;
            break;
        case 12:
            type = THCustomTabBarItemTypeMyJoin;
            break;
    }
    if (self.tabBarItemClickBlock) {
        self.tabBarItemClickBlock(type);
    }
}

- (void)setIsTask:(BOOL)isTask
{
    if (isTask)
    {
        [self.firstBtn setTitle:@"任务大厅" forState:UIControlStateNormal];
    }else
    {
        [self.firstBtn setTitle:@"CPC大厅" forState:UIControlStateNormal];
    }
}


@end
